package com.spic.business.supervision.controller;

import com.spic.business.common.core.workflow.IWorkFlowService;
import com.spic.business.common.core.workflow.WorkFlowController;
import com.spic.business.supervision.domain.HiddenSupervision;
import com.spic.business.supervision.service.IHiddenSupervisionService;
import com.spic.common.core.utils.StringUtils;
import com.spic.common.core.utils.poi.ExcelUtil;
import com.spic.common.core.web.domain.AjaxResult;
import com.spic.common.core.web.page.TableDataInfo;
import com.spic.common.log.annotation.Log;
import com.spic.common.log.enums.BusinessType;
import com.spic.common.security.annotation.PreAuthorize;
import com.spic.system.api.RemoteDeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * 隐患排查督办Controller
 *
 * @author spic
 * @date 2022-05-17
 */
@RestController
@RequestMapping("/hiddenSupervision")
public class HiddenSupervisionController extends WorkFlowController
{
    @Autowired
    private IHiddenSupervisionService hiddenSupervisionService;
    @Autowired
    private RemoteDeptService remoteDeptService;
    /**
     * 查询隐患排查督办列表
     */
    @PreAuthorize(hasPermi = "business:hiddenSupervision:list")
    @GetMapping("/list")
    public TableDataInfo list(HiddenSupervision hiddenSupervision)
    {

        //获取部门及子部门
        if (StringUtils.isNotEmpty(hiddenSupervision.getRectificationDept())){
            List<Long> rectificationDepts = remoteDeptService.getDeptAndChildren(Long.parseLong(hiddenSupervision.getRectificationDept())).getData();
            hiddenSupervision.setRectificationDepts(rectificationDepts);
        }
        startPage();
        List<HiddenSupervision> list = hiddenSupervisionService.selectHiddenSupervisionList(hiddenSupervision);
        return getDataTable(list);
    }

    /**
     * 导出隐患排查督办列表
     */
    @PreAuthorize(hasPermi = "business:hiddenSupervision:export")
    @Log(title = "隐患排查督办", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, HiddenSupervision hiddenSupervision) throws IOException
    {
        List<HiddenSupervision> list = hiddenSupervisionService.selectHiddenSupervisionList(hiddenSupervision);
        ExcelUtil<HiddenSupervision> util = new ExcelUtil<HiddenSupervision>(HiddenSupervision.class);
        util.exportExcel(response, list, "隐患排查督办数据");
    }

    /**
     * 获取隐患排查督办详细信息
     */
    @PreAuthorize(hasPermi = "business:hiddenSupervision:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(hiddenSupervisionService.selectHiddenSupervisionById(id));
    }

    /**
     * 新增隐患排查督办
     */
    @PreAuthorize(hasPermi = "business:hiddenSupervision:add")
    @Log(title = "隐患排查督办", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HiddenSupervision hiddenSupervision)
    {
        return AjaxResult.success(hiddenSupervisionService.insertHiddenSupervision(hiddenSupervision));
    }

    /**
     * 修改隐患排查督办
     */
    @PreAuthorize(hasPermi = "business:hiddenSupervision:edit")
    @Log(title = "隐患排查督办", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HiddenSupervision hiddenSupervision)
    {
        return toAjax(hiddenSupervisionService.updateHiddenSupervision(hiddenSupervision));
    }

    /**
     * 删除隐患排查督办
     */
    @PreAuthorize(hasPermi = "business:hiddenSupervision:remove")
    @Log(title = "隐患排查督办", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(hiddenSupervisionService.deleteHiddenSupervisionByIds(ids));
    }

    @Override
    protected IWorkFlowService getService()
    {
         return hiddenSupervisionService;
    }

    /**
     * 发送短信
     * @param id
     * @return
     */
    @PostMapping("/sendMsg")
    public AjaxResult test(@RequestBody String id){
        hiddenSupervisionService.sendMsg(id);
        return AjaxResult.success();
    }

}
